#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\methods\montecarlo\nodedata.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Methods { namespace Montecarlo {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of INodeData
	public ref class CNodeData  : 
            public Cephei::QL::Methods::Montecarlo::INodeData
	{
	protected: 
		boost::shared_ptr<QuantLib::NodeData>* _ppNodeData;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::NodeData>* _phNodeData;
#endif
		Object^ _NodeDataOwner;     // reference to object that manages the storage for this object
	internal:
        CNodeData (boost::shared_ptr<QuantLib::NodeData>& childNative, Object^ owner);
        CNodeData (QuantLib::NodeData& childNative, Object^ owner);
        CNodeData (CNodeData^ copy);
        CNodeData (PLATFORM::Type^ t);
#ifdef STRUCT
        CNodeData (QuantLib::NodeData childNative);
#endif       
#ifdef HANDLE
		CNodeData (QuantLib::Handle<QuantLib::NodeData>& childNative, Object^ owner);
		CNodeData (QuantLib::Handle<QuantLib::NodeData> childNative);
#endif
		virtual ~CNodeData ();
		!CNodeData ();

	internal:
		QuantLib::NodeData& GetReference ();
		boost::shared_ptr<QuantLib::NodeData>& GetShared ();
		QuantLib::NodeData* GetPointer ();
        void SetNodeData (boost::shared_ptr<QuantLib::NodeData> native)
        {
            if (_ppNodeData != NULL)
                delete _ppNodeData;
            _ppNodeData = new boost::shared_ptr<QuantLib::NodeData> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::NodeData>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of INodeData Vector interface
	ref class CNodeDataVector sealed : 
							    public Core::INativeVector<INodeData^>,
							    public Core::INativeVector<Core::Generic::ICoCell<INodeData^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::NodeData>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::NodeData>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::NodeData>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CNodeDataVector ();
#ifdef STRUCT
		CNodeDataVector (std::vector<QuantLib::NodeData>& vec, Object^ owner);
		CNodeDataVector (std::vector<QuantLib::NodeData> vec);
#endif       
		CNodeDataVector (std::vector<boost::shared_ptr<QuantLib::NodeData>>& vec, Object^ owner);
#ifdef HANDLE
		CNodeDataVector (std::vector<QuantLib::Handle<QuantLib::NodeData>>& vec, Object^ owner);
#endif
		virtual ~CNodeDataVector ();
		!CNodeDataVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property INodeData^ default[int] 
		{
			virtual INodeData^ get (int index) = Core::INativeVector<INodeData^>::default::get;
			virtual void set (int index, INodeData^ value) = Core::INativeVector<INodeData^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<INodeData^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<INodeData^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<INodeData^>^ value) = Core::INativeVector<Core::Generic::ICoCell<INodeData^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::NodeData>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::NodeData>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::NodeData>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Methods::Montecarlo::INodeData^>::typeid)]
	[FactoryFor(Cephei::QL::Methods::Montecarlo::INodeData::typeid)]
	[FactoryFor(Cephei::QL::Methods::Montecarlo::INodeData_Factory::typeid)]
	public ref class CNodeData_Factory sealed : public INodeData_Factory
	{
	public:
        /* <summary> Create a hybrid Vector of INodeData, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<INodeData^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of INodeData, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<INodeData^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<INodeData^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<INodeData^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<INodeData^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<INodeData^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Methods::Montecarlo::INodeData^>^ CreateNativeVector ()
		{
			return REFNEW CNodeDataVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Methods::Montecarlo::INodeData^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Methods::Montecarlo::INodeData^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Methods*/ } /*Montecarlo */}
